//
//  HSTabBarController.swift
//  HomeSchoolDemo
//
//  Created by rimi on 16/9/26.
//  Copyright © 2016年 黄华强. All rights reserved.
//

import UIKit

class HSTabBarController: UITabBarController {
    
    //全局闭包属性
    var leftBtnBlock: (()->())?
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = #colorLiteral(red: 0.1764705926, green: 0.4980392158, blue: 0.7568627596, alpha: 1)
        //改变按钮字体选择颜色
        tabBar.tintColor = UIColor().colorWithHex(0x00a9e9)
        //数据字典数组
        let tabDict = [["vc":"HSMessagesTableViewController","title":"消息","imageName":"inform"],
                       ["vc":"HSHomeWorkTableViewController","title":"作业","imageName":"homework"],
                       ["vc":"HSClassRoomTableViewController","title":"课堂","imageName":"class_exercise"],
                       ["vc":"HSScoreTableViewController","title":"成绩","imageName":"examination"],]
        //通过遍历字典添加controller
        for dict in tabDict {
            addChildController(dict)
        }
        
    }
    ///自定义的添加子控制器的方法
    //fileprivate 私有化，支持延展
    //private完全私有，只在当前类中有效
    fileprivate func addChildController(_ dict: [String:String]) {
        
        //获得系统Info.plist文件
        
        //通过拼接获得完整的class类名
        guard let bundleName = Bundle.main.bundleName,
            let imageName = dict["imageName"],
            let vcName = dict["vc"],
            //通过反射机制将字符串转换为 类 类型
            let cls = NSClassFromString("\(bundleName).\(vcName)") as? UIViewController.Type else{
                return
        }
        
        //获得具体的控制器
        
        let childController = cls.init()
        
//如果不通过反射获取控制器
//        if dict["vc"] == "HSScoreTableViewController" {
//             childController = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSScoreTableViewController")
//        }
        
        //设置控制器的各种属性
        childController.title = dict["title"]
        childController.tabBarItem.image = UIImage(named:"\(imageName)_nor")?.withRenderingMode(.alwaysOriginal)
        //设置标签栏字体大小
        childController.tabBarItem.setTitleTextAttributes([NSFontAttributeName:UIFont.systemFont(ofSize: 14)], for: .normal)
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_press")?.withRenderingMode(.alwaysOriginal)
        //添加左边按钮
        childController.navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "icon_menu_nor", bgImageName: "icon_menu_press", target: self, action: #selector(leftButtonClicked))
        //将视图套上导航栏
        let naviVc = HSNavigationController(rootViewController: childController)
        addChildViewController(naviVc)
    }
    //左边按钮的监听回调方法
    @objc fileprivate func leftButtonClicked() {
        let keyWindow = UIApplication.shared.keyWindow
        keyWindow?.endEditing(true)

        //执行闭包，传递点击事件
        leftBtnBlock?()
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()

    }
    
}
